package com.example.company.controller;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.company.entity.*;
import com.example.company.mapper.DepartmentsMapper;
import com.example.company.service.IDepartmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 存储部门信息 前端控制器
 * </p>
 *
 * @author 88Ra
 * @since 2024-12-31
 */
@RestController
@RequestMapping("/api/system")
public class DepartmentsController {
	
	@Autowired
	private DepartmentsMapper departmentsMapper;
	
	@Autowired
	private IDepartmentsService iDepartmentsService;
	
	@CrossOrigin
	@GetMapping("/department/select")
	public ApiResult finddepartment() {
		List<Departments> departments = new ArrayList<>();
		departments = departmentsMapper.selectDepartment();
		if (!departments.isEmpty()) {
			System.out.println(departments);
			return ApiResultHandler.buildApiResult(200, "请求成功", departments);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	
	
	
	
	
	@GetMapping("/departments/list")
	public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(defaultValue = "") String search) {
		return Result.success(departmentsMapper.selectPage
				(new Page<>(pageNum, pageSize), Wrappers.<Departments>lambdaQuery()
						.like(Departments::getDepartmentname,search)));
	}
	
	
	
	@CrossOrigin
	@PostMapping("/departments")
	public ApiResult RegisterDepartment(@RequestBody Departments departments) {
		int count = departmentsMapper.insert(departments);
		if (count == 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", departments);
			
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	
	
	/**
	 * 查询部门信息
	 *
	 * @param departmentid
	 * @return
	 */
	@GetMapping(value = "/departments/{departmentid}")
	public Departments getInfo(@PathVariable("departmentid") Integer departmentid) {
		Departments departments=iDepartmentsService.selectDepartmentsByDepartmentID(departmentid);
		return departments;
	}
	
	/**
	 * 修改部门信息
	 *
	 * @param departments
	 * @return
	 */
	@PutMapping("departments")
	public ApiResult edit(@RequestBody Departments departments) {
		int cur = iDepartmentsService.updateDepartments(departments);
		if (cur >= 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", cur);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
		
		
	}
	
	
	
	@DeleteMapping("departments/{departmentsids}")
	public ApiResult deletdepartments(@PathVariable Integer[] departmentsids) {
		
		int cur = iDepartmentsService.deleteDepartmentsByDepartmentIDs(departmentsids);
		
		if (cur >= 1) {
			return ApiResultHandler.buildApiResult(200, "请求成功", cur);
		} else {
			return ApiResultHandler.buildApiResult(400, "请求失败", null);
			
		}
	}
	
	
	
}
